var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var bodyParser = require("body-parser");
var https = require('https');
var config = require("../config.json");
var pool = mysql.createPool(config.mysql);
router.use(bodyParser.json({
	limit: "20000 kb"
}));
router.use(bodyParser.urlencoded({
	parameterLimit: 100000,
	limit: '50mb',
	extended: false
}));
/* GET users listing. */
router.get('/allRelease', function(req, res, next) {
	pool.getConnection(function(err, connection) {
		connection.query('select author,title,type,createTime,id,note from acticle', function(err, rows, fields) {

			if (err) {
				console.log(err);
			};
			var json = {
				data: rows
			}
			res.send(json);
			connection.release();
		});

	});

});
/* GET users listing. */
router.get('/getRecommended', function(req, res, next) {
	pool.getConnection(function(err, connection) {
		connection.query('select title,id from acticle where isRecommended=1', function(err, rows, fields) {

			if (err) {
				console.log(err);
			};
			var json = {
				data: rows
			}
			res.send(json);
			connection.release();
		});

	});

});
/* GET users listing. */
router.get('/Release/:id', function(req, res, next) {
	pool.getConnection(function(err, connection) {
		connection.query('select * from acticle where id=' + req.params.id + '', function(err, rows, fields) {
			if (err) {
				console.log(err);
			};
			if (rows.length == 0) {
				var jsonData = {
					data: ""
				}
				res.send("别闹了,没有这个文章");
				return;
			}
			var json = {
				data: rows
			}
			res.send(json.data[0]);
			connection.release();
		});
	});
});
router.post('/toRelease', function(req, res) {
	var author = "'" + req.body.author + "'";
	var title = "'" + req.body.title + "'";
	var type = "'" + req.body.type + "'";
	var time = "'" + req.body.time + "'";
	var num = "'" + req.body.num + "'";
	var msg = "'" + req.body.msg + "'";
	var note = "'" + req.body.note + "'";
	var sql = "INSERT INTO acticle (author,title,type,createTime,content,note) VALUES (" + author + "," + title + "," + type + "," + time + "," + msg + "," + note + ")";
	pool.getConnection(function(err, connection) {
		connection.query(sql, function(err, rows, fields) {
			if (err) throw err;
			var json = {
				data: "发布成功"
			}
			res.send(json);
			connection.release();
		});
	});
});

router.post('/comment', function(req, res) {
	var author = "" + req.body.author + "";
	var acticle = "" + req.body.acticle + "";
	var type = "" + req.body.type + "";
	var msg = "'" + req.body.msg + "'";
	var types = type.split("_")[0];
	var typew = type.split("_")[1];
	var sql = "select * from comments where acticle=" + acticle + " and critics=" + author;
	pool.getConnection(function(err, connection) {
		connection.query(sql, function(err, rows, fields) {
			if (err) throw err;
			if (rows.length == 0) {
				var lsql = "INSERT INTO comments (acticle,critics," + types + ") VALUES(" + acticle + "," + author + "," + typew + ")";
				connection.query(lsql, function(err, rows, fields) {

					res.send("succ");
				})
			}
			if (rows.length == 1) {
				var lsql = "UPDATE comments set " + types + "=" + typew + " where id=" + rows[0].id;
				console.log(lsql)
				connection.query(lsql, function(err, rows, fields) {

					res.send("succ");
				})
			}

			connection.release();
		});
	});
});
router.get('/comment/state', function(req, res) {
	var author = "" + req.query.author + "";
	var acticle = "" + req.query.acticle + "";
	var sql = "select * from comments where acticle=" + acticle + " and critics=" + author;
	pool.getConnection(function(err, connection) {
		connection.query(sql, function(err, rows, fields) {
			if (err) {
				console.log(err)
			};
			if (rows.length == 0) {
				var lsql = "INSERT INTO comments (acticle,critics) VALUES(" + acticle + "," + author + ")";
				connection.query(lsql, function(err, rows, fields) {

					var arr = [{
						collect: 0,
						like: 0,
						unlike: 0
					}]

					res.send(arr);
				})
			}
			if (rows.length == 1) {
				console.log(rows);
				res.send(rows);

			}

			connection.release();
		});
	});

});
module.exports = router;